﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using IToursDataService;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using IToursDataService.Models;
using System.Data;

namespace ToursDataService
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“ExpensesCtrl”。
    public class ExpensesCtrl : IExpensesCtrl
    {
        #region 获取相关费用信息

        /// <summary>
        /// 获取费用状态，看是否存在‘计划’的对账单费用项数据
        /// </summary>
        /// <param name="tourGroupId">团号</param>
        /// <returns>true存在，false不存在</returns>
        public bool GetAllExpenseStatusForBill(string tourGroupId, string loginProviderID)
        {
            Database db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand;
            string Sql = string.Format(@"SELECT TOP 1  ExpenseID FROM fee_Expense WHERE TourgroupID='{0}' AND ProviderID={1} AND Status = '{2}'", tourGroupId, loginProviderID, ExpenseStatusEnum.计划);

            dbCommand = db.GetSqlStringCommand(Sql);

            object retValue = db.ExecuteScalar(dbCommand);
            return retValue == null ? false : true;
        }
        /// <summary>
        /// 获取费用ID，用于对账单
        /// </summary>
        /// <param name="tourgroupId"></param>
        /// <param name="loginProviderID"></param>
        /// <returns></returns>
        public string GetExpenseIDForBill(string tourgroupId, string loginProviderID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand;
            string Sql;

            Sql = string.Format(@"SELECT expenseid FROM fee_Expense WHERE TourgroupID='{0}' AND ProviderID={1}", tourgroupId, loginProviderID);
            dbCommand = db.GetSqlStringCommand(Sql);
            object obj = db.ExecuteScalar(dbCommand);

            if (obj != null)
                return obj.ToString();
            else
                return "";
        }
        /// <summary>
        /// 获取费用状态，用于对账单
        /// </summary>
        /// <param name="tourgroupId"></param>
        /// <param name="loginProviderID"></param>
        /// <returns></returns>
        public string GetExpenseStatusForBill(string tourgroupId, string loginProviderID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand;
            string Sql;

            Sql = string.Format(@"SELECT status FROM fee_Expense WHERE TourgroupID='{0}' AND ProviderID={1}", tourgroupId, loginProviderID);
            dbCommand = db.GetSqlStringCommand(Sql);
            object obj = db.ExecuteScalar(dbCommand);

            if (obj != null)
                return obj.ToString();
            else
                return "";
        }
        #endregion

        #region 费用增、删、改
        /// <summary>
        /// 更新费用的‘审核状态’信息，‘计划、待审核、审核、导游报销、终结’
        /// </summary>
        /// <param name="expenseId">费用标识</param>
        /// <param name="status">被更新成为状态</param>
        /// <returns></returns>
        public int UpdateExpenseStatusData(string expenseId, ExpenseStatusEnum status)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string sqlExpense = string.Format(@"UPDATE fee_Expense SET [Status]='{0}' WHERE ExpenseID={1}", status, expenseId);
            return db.ExecuteNonQuery(CommandType.Text, sqlExpense);
        }

        #endregion
    }
}
